/**
 * @author  guocailee
 */

let setItem = (key, data) => {
  if (!window.localStorage || !window.JSON || !key) {
    return
  }
  window.localStorage.setItem(key, JSON.stringify(data))
}

let getItem = (key) => {
  if (!window.localStorage || !window.JSON || !key) {
    return
  }
  let item = window.localStorage.getItem(key)
  if (!item) {
    return
  }
  return JSON.parse(item)
}

let removeItem = (key) => {
  if (!window.localStorage || !window.JSON || !key) {
    return
  }
  window.localStorage.removeItem(key)
}

let available = !!window.localStorage

export default {
  setItem,
  getItem,
  removeItem,
  available
}
